<template>
	<div class="page-wrapper">
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-input v-model="state.queryForm.name" placeholder="名称" clearable style="width: 200px;"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="getDataList()" type="primary">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button @click="clearForm()">重置</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="addOrUpdateHandle()" v-if="hasRule('admin/supplier/index/add')">新增</el-button>
			</el-form-item>
			<!-- <el-form-item>
				<el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
			</el-form-item> -->
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%" width="100%" height="100%">
            <!-- @selection-change="selectionChangeHandle"  -->
			<!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
			<el-table-column prop="created_at" label="创建时间" header-align="center" align="center" width="180"></el-table-column>
			<el-table-column prop="name" label="供应商名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="username" label="供应商联系人" header-align="center" align="center"></el-table-column>
			<el-table-column prop="phone" label="联系电话" header-align="center" align="center"></el-table-column>
			<el-table-column prop="address" label="地址" header-align="center" align="left">
                <template #default="scope">
					<div class="w100 hh">{{scope.row.address}}</div>
				</template>
            </el-table-column>
			<el-table-column prop="remark" label="备注" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="250">
				<template #default="scope">
					<el-button type="success" link @click="handleCheck(scope.row, 1)" v-if="hasRule('admin/supplier/index/in')">查看入库记录</el-button>
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)" v-if="hasRule('admin/supplier/index/update')">修改</el-button>
					<el-button type="danger" link @click="deleteHandle(scope.row.id)" v-if="hasRule('admin/supplier/index/delete')">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.per_page"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
        <el-dialog v-model="visible" :title="title" width="1200px" center :destroy-on-close="true">
           <div class="w100">
            <el-scrollbar height="600px">
                <el-table :data="itemList" border style="width: 100%" width="100%" height="100%">
                    <el-table-column prop="order_sn" label="入库单号" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="in_time" label="入库时间" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="in_type" label="入库类型" header-align="center" align="center">
                        <template #default="scope">
                            <span v-if="scope.row.in_type == 1">产品采购入库</span>
                            <span v-if="scope.row.in_type == 2">材料采购入库</span>
                            <span v-if="scope.row.in_type == 3">退货入库</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="product_name" label="入库产品" header-align="center" align="center">
                        <template #default="scope">
                            <span>{{ scope.row.product_name }}-{{ scope.row.product_attr }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quantity" label="入库数量" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="operator" label="操作人" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="created_at" label="操作时间" header-align="center" align="center"></el-table-column>
                </el-table>
            </el-scrollbar>
           </div>
        </el-dialog>
	</div>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import AddOrUpdate from './add-or-update.vue'
import { IHooksOptions } from '@/hooks/interface'
import { getInventoryItem } from '@/api/api'
import { hasRule } from '@/utils/tool'

const state: IHooksOptions = reactive({
	dataListUrl: '/api/strator/supplier/list',
	deleteUrl: '/api/strator/supplier/delete',
	queryForm: {
		name: ''
	}
})

const title = ref('');
const visible = ref(false);
const itemList = ref([]);
const clearForm = () => {
    state.queryForm.name = '';
    getDataList();
}
const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number, isRole?: any) => {
	addOrUpdateRef.value.init(id, isRole)
}
const handleCheck = (row, type) => {
    if(type==1){
        title.value = row.name + '入库记录';
    }else{
        title.value = row.name + '出库记录';
    }
    itemList.value = [];
    visible.value = true;
    getInventoryItem({
        supplier_name: row.name,
        type: type,
    }).then((result) => {
        itemList.value = result.data;
    }).catch((err) => {
        
    });
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteHandle } = useCrud(state)
</script>
